<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="">
<head>
    <title></title>
    <!-- jqGrid组件基础样式包-必要 -->
    <link rel="stylesheet" th:href="@{/admin/dist/js/jqgrid-5.3.0/ui.jqgrid.css}"/>
</head>

<style>
    .ui-jqgrid tr.jqgrow td {
        white-space: normal !important;
        height: auto;
        vertical-align: text-top;
        padding-top: 2px;
        line-height: 45px;
    }
</style>
<header th:replace="admin/header::header-fragment"></header>
<body class="hold-transition sidebar-mini">
<div class="wrapper">
    <!-- 引入页面头header-fragment -->
    <div th:replace="admin/header::header-nav"></div>
    <!-- 引入工具栏sidebar-fragment -->
    <div th:replace="admin/sidebar::sidebar-fragment(${path})"></div>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Content Header (Page header) -->
        <div class="content-header">
            <div class="container-fluid">
            </div><!-- /.container-fluid -->
        </div>
        <!-- Main content -->
        <div class="content">
            <div class="container-fluid">
                <div class="card card-primary card-outline">
                    <div class="card-header">
                        <h3 class="card-title">评论管理</h3>
                    </div> <!-- /.card-body -->
                    <div class="card-body">
                        <div class="grid-btn">
                            <button class="btn btn-success" onclick="checkDoneComments()"><i
                                    class="fa fa-check"></i>&nbsp;批量审核
                            </button>
                            <button class="btn btn-info" onclick="reply()"><i
                                    class="fa fa-reply"></i>&nbsp;回复
                            </button>
                            <button class="btn btn-danger" onclick="deleteComments()"><i
                                    class="fa fa-trash-o"></i>&nbsp;批量删除
                            </button>
                        </div>
                        <!-- JqGrid必要DOM,用于创建表格展示列表数据 -->
                        <table id="jqGrid" class="table table-bordered"></table>
                        <!-- JqGrid必要DOM,分页信息区域 -->
                        <div id="jqGridPager"></div>
                    </div><!-- /.card-body -->
                </div>
            </div><!-- /.container-fluid -->
        </div>
        <!-- /.content -->
        <div class="content">
            <!-- 模态框（Modal） -->
            <div class="modal fade" id="replyModal" tabindex="-1" role="dialog" aria-labelledby="replyModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h6 class="modal-title" id="replyModalLabel">评论回复</h6>
                        </div>
                        <div class="modal-body">
                            <form id="replyForm">
                                <input type="hidden" class="form-control" id="categoryId" name="categoryId">
                                <div class="form-group">
                                    <label for="replyBody" class="control-label">回复内容:</label>
                                    <textarea type="text" class="form-control" id="replyBody" name="replyBody"
                                              placeholder="请输入回复内容" required="true"></textarea>
                                </div>
                            </form>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                            <button type="button" class="btn btn-primary" id="saveButton">确认</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.modal -->
        </div>
    </div>
    <!-- /.content-wrapper -->
    <!-- 引入页脚footer-fragment -->
    <div th:replace="admin/footer::footer-fragment"></div>
</div>
<!-- jQuery -->
<script th:src="@{/admin/dist/js/jquery/jquery-3.5.1.min.js}"></script>
<!-- AdminLTE App -->
<script src="/admin/dist/js/adminlte.min.js"></script>
<script src="/admin/dist/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>

<!-- jqgrid -->
<script th:src="@{/admin/dist/js/jqgrid-5.3.0/jquery.jqGrid.min.js}"></script>
<script th:src="@{/admin/dist/js/jqgrid-5.3.0/grid.locale-cn.js}"></script>
<script th:src="@{/admin/dist/plugins/sweetalert/sweetalert.min.js}"></script>
<script>
    $(function () {
        $("#jqGrid").jqGrid({
            url: '/admin/comment/list',
            mtype: "get",
            datatype: "json",
            jsonReader: {
                root: "data.list",
                total: "data.pages",
                page: "data.pageNum",
                records: "data.total"
            },
            colNames: ['id', '内容', '核审', '邮箱', '回复时间'],
            //jqgrid主要通过下面的索引信息与后台传过来的值对应
            colModel: [
                {name: 'id', index: 'id', hidden: true,},
                {name: 'content', index: 'content', align: "center",},
                {name: 'commentStatus', index: 'commentStatus', align: "center", formatter: statusFormatter},
                {name: 'email', index: 'email', align: "center"},
                {
                    name: 'time',
                    index: 'time',
                    align: "center",
                    formatter: 'date',
                    formatoptions: {srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s'}
                },   //没有srcformat: 'Y-m-d H:i:s',时分秒就会使00:00:00
            ],
            height: 470,
            rowNum: 10,
            rowList: [10, 20, 50],
            multiselect: true,
            autowidth: true,
            styleUI: 'Bootstrap',
            pager: "#jqGridPager",
            // caption: "Cell Edit Example",
        });

        function statusFormatter(cellvalue) {
            if (cellvalue > 0) {
                return "<button type=\"button\" class=\"btn btn-block btn-success btn-sm\" style=\"width: 25%;display: inline;\">已审核</button>";
            } else {
                return "<button type=\"button\" class=\"btn btn-block btn-secondary btn-sm\" style=\"width: 25%;display: inline;\">待审核</button>";
            }
        }
    });

    //核审
    function checkDoneComments() {
        let ids = getSelectRow();
        if (ids.length === 0) {
            swal("请先勾选核审的行");
            return;
        }

        $.ajax({
            url: "/admin/comment/check",
            type: "post",
            datatype: "json",
            contentType: "application/json",
            data: JSON.stringify(ids),
            success: function (data) {
                if (data.code === 1000) {
                    swal("核审成功", {
                        icon: "success"
                    });
                    $("#jqGrid").trigger("reloadGrid");
                } else {
                    swal("核审失败", {
                        icon: "error"
                    });
                }
            }
        })
    }

    //回复
    function reply() {
        swal("111");
        return;
        let ids = getSelectRow();
        if (ids.length === 0) {
            alert("请勾选要回复的评论");
            return;
        }
        if (ids.length > 1) {
            alert("只能回复逐个回复");
            return;
        }

        let comment = "";

        $.ajax({
            url: "/admin/comment/reply",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(comment),
            success: function (data) {
                if (data.code === 200) {
                    alert("回复成功");
                    $("#jqGrid").trigger("reloadGrid");
                } else {
                    alert("回复失败");
                }
            }
        });

    }

    //删除
    function deleteComments() {
        let ids = getSelectRow();
        if (ids.length === 0) {
            alert("请勾选要删除的评论");
            return;
        }
        $.ajax({
            url: "/admin/comment/delete",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(ids),
            success: function (data) {
                if (data.code === 1000) {
                    swal("删除成功", {
                        icon: "success"
                    });
                    $("#jqGrid").trigger("reloadGrid");
                } else {
                    swal("删除失败", {
                        icon: "error"
                    });
                }
            }
        })
    }

    function getSelectRow() {
        return $("#jqGrid").getGridParam("selarrrow");
    }

</script>
</body>
</html>
